/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.quantanetwork.esm.port;

import java.io.Serializable;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * 
 * @author Administrator
 */
public class UdpSocket implements Serializable
{
	private static final long serialVersionUID = 7984283201050292218L;
	private DatagramSocket datagramSocket = null;
	private int listen = 0;
	private int datagramPacketSize = 1024 * 8;
	private int timeOut = 2000;

	public String getLocalAddress()
	{
		String rtnVal = "";
		if (datagramSocket != null)
		{
			rtnVal = datagramSocket.getLocalAddress().getHostAddress();
		}
		return rtnVal;
	}

	public int getLocalPort()
	{
		int rtnVal = 0;
		if (datagramSocket != null)
		{
			rtnVal = datagramSocket.getLocalPort();
		}
		return rtnVal;
	}

	protected void openUdpSocket() throws SocketException
	{
		if (datagramSocket == null || datagramSocket.isClosed())
		{
			datagramSocket = new DatagramSocket(listen);
		}
		if (datagramSocket.getSoTimeout() != timeOut)
		{
			datagramSocket.setSoTimeout(timeOut);
		}
	}

	protected void closeUdpSocket()
	{
		if (datagramSocket != null && datagramSocket.isBound())
		{
			datagramSocket.close();
		}
		datagramSocket = null;
	}

	public DatagramSocket getDatagramSocket()
	{
		return datagramSocket;
	}

	public void setDatagramSocket(DatagramSocket datagramSocket)
	{
		this.datagramSocket = datagramSocket;
	}

	public int getListen()
	{
		return listen;
	}

	public void setListen(int listen)
	{
		this.listen = listen;
	}

	public int getDatagramPacketSize()
	{
		return datagramPacketSize;
	}

	public void setDatagramPacketSize(int datagramPacketSize)
	{
		this.datagramPacketSize = datagramPacketSize;
	}

	public int getTimeOut()
	{
		return timeOut;
	}

	public void setTimeOut(int timeOut)
	{
		this.timeOut = timeOut;
	}
}
